#encoding: utf-8
from .startDB import sparrowDB
from .getLogs import collectLogs
import traceback

# 获取测试用例，通过sql
def getCases_sql(groupId):
    groupId = str(groupId)
    try:
        sql = 'select * from case_manage where isDel = 0 and status = 1 and group_id = %s' % (groupId,)
        collectLogs(loginfo='获取用例的sql：%s' %(sql,), logger='getCases')
        db_res = sparrowDB.get_all(sql)
        if db_res:
            return db_res
        else:
            collectLogs(loginfo='没有查询到用例，查询结果：%s' % (db_res,), lever='warning', logger='getCases')
            return False
    except:
        collectLogs(loginfo='用例获取异常，原因：%s' %(traceback.format_exc(),), lever='error', logger='getCases')
        return False

# 获取用例，通过数据库模型
def getCases_model(casemanage, groupId):
    groupId = str(groupId)
    try:
        cases = casemanage.query.filter(casemanage.isDel == 0, casemanage.status == 1, casemanage.group_id == groupId)
        if cases:
            return cases
        else:
            collectLogs(loginfo='没有查询到用例，查询结果：%s' % (cases,), lever='warning', logger='getCases')
            return False
    except:
        collectLogs(loginfo='用例获取异常，原因：%s' %(traceback.format_exc(),), lever='error', logger='getCases')
        return False


